/**
 * 添加描述
 *
 * @author Your ID
 * @since 2025−04-27
 *
 * Copyright © Huawei Technologies Co., Ltd.2025−2025. All rights reserved.
 */
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';
import { Permissions } from '@ohos.abilityAccessCtrl';
import { BusinessError } from '@ohos.base';
import { common, bundleManager } from '@kit.AbilityKit';

interface PositioningRelatedPermission {
  permissionName: string;
  granted: boolean;
}

interface GeneratedObjectLiteralInterface_1 {
  FINE_LOCATION: Permissions;
  COARSE_LOCATION: Permissions;
  BACKGROUND_LOCATION: Permissions;
  NETWORK_STATE: Permissions;
  WIFI_STATE: Permissions;
}

export class PermissionUtil {
  // 权限常量映射表
  private static readonly LOCATION_PERMISSIONS: GeneratedObjectLiteralInterface_1 = {
    FINE_LOCATION: 'ohos.permission.LOCATION',
    COARSE_LOCATION: 'ohos.permission.APPROXIMATELY_LOCATION',
    BACKGROUND_LOCATION: 'ohos.permission.LOCATION_IN_BACKGROUND',
    NETWORK_STATE: 'ohos.permission.GET_NETWORK_INFO',
    WIFI_STATE: 'ohos.permission.GET_WIFI_INFO'
  };

  static async checkPermissions(): Promise<PositioningRelatedPermission[]> {
    return [
      await PermissionUtil.fineLocation(),
      await PermissionUtil.networkState(),
      await PermissionUtil.wifiState(),
      await PermissionUtil.backGroundLocation(),
      await PermissionUtil.coarseLocation()
    ];
  }

  private static async getDeviceTokenId(): Promise<number> {
    let applicationBundleInfo =
      await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION)
    return applicationBundleInfo.appInfo.accessTokenId
  }

  private static async checkPermission(permission: Permissions): Promise<PositioningRelatedPermission> {
    try {
      const atManager = abilityAccessCtrl.createAtManager();
      return {
        permissionName: permission,
        granted: await atManager.checkAccessToken(await PermissionUtil.getDeviceTokenId(), permission) ===
        abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED
      };
    } catch (error) {
      const err: BusinessError = error as BusinessError;
      console.error(`Check permission failed, code: ${err.code}, message: ${err.message}`);
      return { permissionName: permission, granted: false };
    }
  }

  private static async networkState(): Promise<PositioningRelatedPermission> {
    return await PermissionUtil.checkPermission(PermissionUtil.LOCATION_PERMISSIONS.NETWORK_STATE);
  }

  private static async fineLocation(): Promise<PositioningRelatedPermission> {
    return await PermissionUtil.checkPermission(PermissionUtil.LOCATION_PERMISSIONS.FINE_LOCATION);
  }

  private static async coarseLocation(): Promise<PositioningRelatedPermission> {
    return await PermissionUtil.checkPermission(PermissionUtil.LOCATION_PERMISSIONS.COARSE_LOCATION);
  }

  private static async backGroundLocation(): Promise<PositioningRelatedPermission> {
    return await PermissionUtil.checkPermission(PermissionUtil.LOCATION_PERMISSIONS.BACKGROUND_LOCATION);
  }

  private static async wifiState(): Promise<PositioningRelatedPermission> {
    return await PermissionUtil.checkPermission(PermissionUtil.LOCATION_PERMISSIONS.WIFI_STATE);
  }
}

//export default PermissionUtil;